import { ExerciseTypeEnum } from '@/api/constants'
import { defineStore } from 'pinia'
import { reactive, ref } from 'vue'

// 答题-语音播放选项-枚举

enum AnswerSoundPlayOptionEnum {
  'Topic',
  'Topic_Option',
  'Topic_Answer'
}
// 定义 useSettingStore 保存用户的设置信息
export const useSettingStore = defineStore(
  'setting',
  () => {

    /**
     * 答题切换下一题
     */
    const answerCorrectAutoPre = ref<boolean>(true)
    /**
     * 答题-提示音
     */
    const answerTipSound = ref<boolean>(false)
    /**
     * 答题-语音播放
     */
    const answerSoundPlay = ref<AnswerSoundPlayOptionEnum>(AnswerSoundPlayOptionEnum.Topic)

    /**
     * 答题技巧-悬浮按钮
     */
    const answerSkillBtn = ref<boolean>(true)
    /**
     * 字号
     */
    const fontSize = ref<number>(36)
    /**
     * 显示模式
     */
    const seeMode = ref<string>('')

    // 自动移除错题配置
    const autoRemoveErrorQuestion = ref({
      /**
       * 自动易错错题
       */
      autoRemove: true,
      /**
       *答对次数
       */
      answerCorrectNum: 3
    })

    /**
     * 错题详情-分组索引
     */
    const errorQuestionGroupIndex = ref<number>(ExerciseTypeEnum.AnswerErrorNumExercise)

    /**
   * 收藏详情-分组索引
   */
    const collectQuestionGroupIndex = ref<number>(ExerciseTypeEnum.ChapterCollectQuestionExercise)
    /**
     * 设置-答题-答对-切换下一题
     * @param val
     */
    const setAnswerCorrectAutoPre = (val: boolean) => {
      answerCorrectAutoPre.value = val
    }

    /**
     * 设置-答题-提示音
     * @param val
     */
    const setAnswerTipSound = (val: boolean) => {
      answerTipSound.value = val
    }

    /**
     * 设置-答题-语音播放
     */
    const setAnswerSoundPlay = (val: AnswerSoundPlayOptionEnum) => {
      answerSoundPlay.value = val
    }

    /**
     * 设置-答题技巧-悬浮按钮
     * @param val
     */
    const setAnswerSkillBtn = (val: boolean) => {
      answerSkillBtn.value = val
    }
    /**
     *
     * @param val 设置字号
     */
    const setFontSize = (val: number) => {
      fontSize.value = val
    }


    /**
     *设置显示模式
     * @param val
     */
    const setSeeMode = (val: string) => {
      seeMode.value = val
    }

    /**
     * 设置自动移除错题
     * @param autoRemove
     */
    const setAutoRemoveErrorQuestion = (autoRemove: boolean) => {
      autoRemoveErrorQuestion.value.autoRemove = autoRemove
    }

    /**
     * 设置自动移除错题答对次数
     * @param answerCorrectNum
     */
    const setAutoRemoveErrorQuestionAnswerCorrectNum = (answerCorrectNum: number) => {
      autoRemoveErrorQuestion.value.answerCorrectNum = answerCorrectNum
    }

    /**
     * 设置错题详情-分组索引
     * @param val
     */
    const setErrorQuestionGroupIndex = (val: ExerciseTypeEnum) => {
      errorQuestionGroupIndex.value = val
    }

    /**
     * 设置收藏详情-分组索引
     * @param val 
     */
    const setCollectQuestionGroupIndex = (val: ExerciseTypeEnum) => {
      collectQuestionGroupIndex.value = val
    }
    // 记得 return
    return {
      answerCorrectAutoPre,
      answerTipSound,
      answerSoundPlay,
      answerSkillBtn,
      fontSize,
      seeMode,
      autoRemoveErrorQuestion,
      errorQuestionGroupIndex,
      collectQuestionGroupIndex,
      setAnswerCorrectAutoPre,
      setAnswerTipSound,
      setAnswerSoundPlay,
      setAnswerSkillBtn,
      setFontSize,
      setSeeMode,
      setAutoRemoveErrorQuestion,
      setAutoRemoveErrorQuestionAnswerCorrectNum,
      setErrorQuestionGroupIndex,
      setCollectQuestionGroupIndex,
    }
  },
  // TODO: 持久化
  {
    // persist: true,
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
      },
    },
  },
)
